
/***************************************************************************************
****************************************************************************************
* FILE		: common.c
* Description	: 
*			  
* Copyright (c) 2020 by Simlife. All Rights Reserved.
* 
* History:
* Version		Name		Date				Description
   0.1		Alen			2020/05/07		Initial Version
   
****************************************************************************************
****************************************************************************************/

#include "includes.h"

#define AUTO_MODE_TIME 90

U16 getAdc12Bit(void)
{

	U8 i = 0xff;
	unionUnsignedInt val;
	_nop();
	_start = 0;
	_nop();
	_start = 1;
	_nop();
	_start = 0;

	while (i--)
	{
		_nop();
		_nop();
		_nop();
		if (!_adbz)
			break;
	}
	if (i == 0)
	{
		val.word = 0xffff;
	}
	else
	{
		val.bytes.byte0 = _sadol;
		val.bytes.byte1 = (_sadoh & B0000_1111);
	}
	return val.word;
}

void modeSet(void)
{
	fillBufferWithValue((U8 *)(&stVibration), sizeof(stVibration), 0);

	stTime.countGreenStep = 0;
	stTime.countGreenTimes = 0;

	stTime.countLEDFLASH = 2 * stSystem.systemMode;
	flagAllMode = FALSE;
	switch (stSystem.systemMode)
	{
	case OMNIPOTENT_MODE:
		POWER_LED_ON();
		flagModeChange = TRUE;
		stTime.countModeChangeTimes = 120;

		stSystem.vibrationMode = VIBRATION_MODE1;
		stSystem.greenMode = GREEN_MODE1;
		stSystem.heatMode = HEAT_MODE_1;
		break;

	case CLEAR_MODE:
		POWER_LED_ON();
		flagModeChange = TRUE;
		stTime.countModeChangeTimes = 120;

		stSystem.vibrationMode = VIBRATION_MODE_OFF;
		closeVibrationAllPin();
		stSystem.greenMode = GREEN_MODE1;
		stSystem.heatMode = HEAT_MODE_1;
		break;

	case RELAX_MODE:
		POWER_LED_ON();
		flagModeChange = FALSE;
		flagAllMode = TRUE;
		stTime.countModeChangeTimes = 0;

		stSystem.vibrationMode = VIBRATION_MODE2;
		stSystem.greenMode = GREEN_MODE_OFF;
		closeGreenNumberPin();
		stSystem.heatMode = HEAT_MODE_1;
		break;

	case ALL_MODE:
		POWER_LED_ON();
		flagModeChange = TRUE;
		flagAllMode = TRUE;
		stTime.countModeChangeTimes = 0;

		stSystem.vibrationMode = VIBRATION_MODE2;
		stSystem.greenMode = GREEN_MODE1;
		stSystem.heatMode = HEAT_MODE_2;
		break;

	default:
		break;
	}
	hotCompressSet();
}

void modeChange(void)
{
	stSystem.systemMode++;
	if (stSystem.systemMode > SYSTEM_MAX_MODE)
	{
		stSystem.systemMode = OMNIPOTENT_MODE;
	}
	modeSet();
}

#define AUTO_CLOSE_POWER_TIMES 1200

void systemPower(U8 status)
{
	if (status)
	{
		START_UART();
		_txen = 1;
		_rxen = 1;
		wifi_ON();
		stTime.countCloseSystem = AUTO_CLOSE_POWER_TIMES;
		stSystem.systemMode = OMNIPOTENT_MODE;
		modeSet();
	}
	else
	{
		stTime.countReadyClose = GOTO_SLEEP_TIME;
		closeHotCompressPin();
		closeLEDPin();
		closeVibrationAllPin();
		closeGreenNumberPin();
		sendModulePowerOff(); //发送
	}
}

void controlCloseSystem(void) //500ms
{
	stTime.countCloseSystem--;
	if (stTime.countCloseSystem == 0)
	{
		flagSystemStatus = OFF;
		systemPower(OFF);
	}
}